<template>
	<view class="window-top">
		<view class="window-flex">
			<view v-if="currentTabbar === 0" class="window-box">
				<scroll-view class="window-scroll" scroll-y @scrolltolower="tabbarPageScrollLower">
					<mallt ref="mallt" :bg="BgMallt" :banner="banner" @nerTop="backNerTop" @navUserEvent="swtouser(4)">
					</mallt>
				</scroll-view>
			</view>

			<view v-if="currentTabbar === 1" class="window-box">
				<scroll-view class="window-scroll" scroll-y @scrolltolower="tabbarPageScrollLower">
					<cart ref="cart" :bg="BgCart" @nerTop="backNerTop"></cart>
				</scroll-view>
			</view>

			<view v-if="currentTabbar === 2" class="window-box">
				<scroll-view class="window-scroll" scroll-y :scroll-top="scrollPoint"
					@scrolltolower="tabbarPageScrollLower">
					<home ref="home" @nerTop="backNerTop"></home>
				</scroll-view>
			</view>

			<view v-if="currentTabbar === 3" class="window-box">
				<scroll-view class="window-scroll" scroll-y enable-back-to-top @scrolltolower="tabbarPageScrollLower">
					<ring ref="ring" :banner="banner"></ring>
				</scroll-view>
			</view>

			<view v-if="currentTabbar === 4" class="window-box">
				<scroll-view class="window-scroll" :scroll-y="false" enable-back-to-top
					@scrolltolower="tabbarPageScrollLower">
					<person ref="person"></person>
				</scroll-view>
			</view>

			<tn-tabbar v-model="currentTabbar" fixed switch-animation @change="switchTabbar" height="50px">
				<tn-tabbar-item v-for="(item, index) in tabbarData" :key="index" :icon="item.icon"
					:active-icon="item.activeIcon" :text="item.name" active-color="#000"
					:bulge="index === 2"></tn-tabbar-item>
			</tn-tabbar>
		</view>
	</view>

	<!-- popup 弹窗-->
	<tn-popup v-model="showBind" radius="0" bg-color="transparent">
		<view class="tn-gradient-bg__cool-14 tn-bg-image top-font-title"
			style="border-radius: 60px 0 60px 0;padding: 20px;color: #fff;display: flex;flex-direction: column;align-items: center;gap: 10px;">
			<view style="font-size: large;">悦玺云商</view>
			<view class="">  本商城为会员制，您尚未收到会员邀请。 </view>			
			<button plain style="margin-top: 20px;">加入会员</button>
		</view>
	</tn-popup>
	<tn-popup v-model="showLevel" radius="0" bg-color="transparent">
		<view class="tn-gradient-bg__cool-14">
			aaaaaaaaaaaaaaaaaa
		</view>
	</tn-popup>
</template>

<script>
	import utils from '@/common/utils.js'
	export default {
		data() {
			return {
				topH: 0,
				title: 'index',
				sConf: getApp().globalData,
				currentTabbar: 2,
				// 推荐链接
				staff: null,
				curStaff: null,
				// 其他参数
				scrollPoint: 100,
				scrollPointOne: 100,
				scrollPointTow: 100,
				// 自定义底栏对应页面的加载情况
				tabberPageLoadFlag: [],
				tabbarData: [{
						name: '悦玺商城',
						icon: 'shop',
						activeIcon: 'shop-fill',
					},
					{
						name: '分类',
						icon: 'menu-classify',
						activeIcon: 'menu-classify-fill',
					},
					{
						name: '首页',
						icon: 'home-smile',
						activeIcon: 'home-smile-fill',
					},
					{
						name: '动态',
						icon: 'footprint',
						activeIcon: 'footprint-fill',
					},
					{
						name: '个人中心',
						icon: 'identity',
						activeIcon: 'identity-fill',
					}
				],
				// 组件数据
				BgMallt: null, // 商城头图背景
				BgCart: null, //  分类
				banner: null, // banner				
				showBind: false, // 弹窗绑定会员
				showLevel: false, // 弹窗会员权益				
			};
		},
		onLoad(args) {
			let staff = Number(args.staff) || null;
			this.staff = staff
			console.log("onLoad", 'index', staff);
			// 隐藏导航栏
			// uni.hideTabBar({});

			// 加载登录信息
			// try {
			// 	const stamp = Date.now();
			// 	const token = uni.getStorageSync('token');
			// 	const auth = uni.getStorageSync('auth');
			// 	const last = uni.getStorageSync('last');
			// 	console.log('加载本地缓存', auth, last);
			// 	if (token && auth && ((stamp - last) < 10 * 3600 * 1000)) {
			// 		console.log('yes');
			// 		this.sConf.token = token;
			// 		this.sConf.auth = auth;
			// 	} else {
			// 		console.log('no');
			// 		uni.clearStorageSync();
			// 		uni.reLaunch({
			// 			url: '/pages/login/login'
			// 		})
			// 	}
			// } catch (e) {
			// 	console.log('加载本地缓存失败');
			// }

			// 加载数据
			utils.httpRequestOnList(this.sConf.host + '/api/conf/', {
				key: 'tt_bg_mallt'
			}).then((res) => {
				console.log('加载头图——页面0');
				this.BgMallt = res.find(item => item.key === 'tt_bg_mallt').value;
			}).catch((err) => {
				console.log('加载失败__tt_bg_mallt');
			});

			utils.httpRequestOnList(this.sConf.host + '/api/conf/', {
				key: 'tt_bg_cart'
			}).then((res) => {
				console.log('加载头图——页面1');
				this.BgCart = res.find(item => item.key === 'tt_bg_cart').value;
			}).catch((err) => {
				console.log('加载失败__tt_bg_cart');
			});

			utils.httpRequestOnList(this.sConf.host + '/api/image/', {
				mode: 'swiper',
				cur: 0,
				status: 1,
				ordering: '-sort'
			}).then((res) => {
				console.log('加载轮播——页面0');
				let tmpData = []
				res.forEach(item => {
					tmpData.push(item.url)
				})
				this.banner = tmpData
			}).catch((err) => {
				console.log('加载失败', err);
			});

			utils.authenticatorOnRead(this.sConf.host + '/weapps/userinfo/' + this.sConf.auth.id + '/', this.sConf.token)
				.then(res => {
					console.log('个人信息', res);
					this.curStaff = res;
					// if (staff !== null && res.parent === null) {
					// 	this.bindParentStaff(staff);
					// }
				})
		},
		onShow() {
			console.log("onShow", 'index');
		},
		onReady() {
			console.log("onReady", 'index');
			// 微信用户授权
			// uni.authorize({
			// 	scope: 'scope.userInfo'
			// });	
			this.$refs.home.loadDefData();

			if (this.staff !== null) {
				console.log('导航到用户页', this.staff);
				let args = encodeURIComponent('parent=' + this.staff)
				uni.navigateTo({
					url: '/pageb/staff/staff?scene=' + args
				})
			}

		},
		methods: {
			swtouser(e) {
				// #ifdef MP-WEIXIN				
				wx.vibrateShort();
				// #endif
			},
			/**
			 * 绑定推荐
			 */
			bindParentStaff(e) {
				let host = this.sConf.host + '/weapps/bindHelp/';
				let token = this.sConf.token || ''
				let data = {
					parent: e
				}
				utils.authenticatorOnPost(host, token, data).then(res => {
					console.log('绑定推荐官');
				})
			},
			homeScrollEnd() {
				this.scrollPoint += 0.01
			},
			pageScrollLower(e) {
				// console.log('pageScrollLower', e);
			},
			tabbarPageScrollLower(e) {
				console.log('tabbarPageScrollLower');
			},
			backNerTop(e) {
				console.log('返回顶部距离', e);
				this.topH = e
				uni.pageScrollTo({
					scrollTop: e,
					duration: 300,
				})
			},
			// 切换导航
			switchTabbar(index) {
				console.log("switchTabbar", index);
				if (index === 0) {
					this.$refs.mallt.loadDefData()
					this.EventBindParent()
					// this.EventBuyLevel()
				}
				if (index === 1) {
					this.$refs.cart.loadDefData()
				}
				if (index === 2) {
					this.$refs.home.loadDefData()
				}
				if (index === 3) {
					this.$refs.ring.loadDefData()
				}
				if (index === 4) {
					this.$refs.person.loadDefData()
				}
			},
			/**
			 * 绑定会员
			 */
			EventBindParent() {
				if (!this.curStaff.parent) {
					this.showBind = !this.showBind
				}
			},
			/**
			 * 会员权益
			 */
			EventBuyLevel() {
				if (this.curStaff.curLevel.level === 1) {
					this.showLevel = !this.showLevel
				}
			}
		}
	}
</script>
<style lang="scss" scoped>
	/* start 全屏适配 */
	.window-top {
		width: 100%;
		height: 100vh;
		display: flex;
		flex-direction: column;
		overflow: hidden;
	}

	.window-flex {
		flex: 1;
		overflow: hidden;
	}

	.window-box {
		height: 100%;
		overflow: hidden;
	}

	.window-scroll {
		height: 100%;
		overflow: hidden;
	}

	/* end 全屏适配 */
</style>